/*
* File: IFdEventInterface-Inl.h
* Copyright (c) 2011. Silviu Caragea <silviu@intelliproject.net>
* Created on January 30, 2011, 11:09 PM
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*  1.Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*  2.Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*  3.All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*       This product includes software developed by the University of
*       California, Berkeley and its contributors.
*  4.Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef IFDEVENTINTERFACE_INL_H
#define	IFDEVENTINTERFACE_INL_H

#include "IFdEventInterface.h"

namespace litevent
{

inline fdnode* IFdEventInterface::_getNode(int fd)
{
    if((size_t)fd >= this->maxfds)
    {
        util::CLogger::sharedLogger()->WriteFormated("%s.%d: fd over maxfds (%d > %d) ", __FILE__, __LINE__, fd, this->maxfds);
        return NULL;
    }

#ifdef USE_STL_MAP
    fdnode *node = NULL;
    FileDescriptorMap::iterator it = this->fdMap.find(fd);

    if(it != fdMap.end())
        node = it->second;
#else
    fdnode *node = this->fdarray[fd];
#endif

    if(!node)
    {
        node = (fdnode*) calloc(1, sizeof(*node));
#ifdef USE_STL_MAP
        this->fdMap[fd]= node;
#else
        this->fdarray[fd] = node;
#endif
        node->nodeIndex = -1;
    }

    return node;
}

template <class K> int IFdEventInterface::fdRegisterEvent(int fd, void*ctx, K *obj, void(K::*method)(int, void*,int) )
{
    /*http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12*/

    fdnode *node = this->_getNode(fd);

    if(!node)
        return 0;

    if(!node->OnEvent)
    {
        node->OnEvent = new SignalEvent;
        node->OnEvent->connect(obj, method);
    }

    node->fd      = fd;
    node->ctx     = ctx;
    node->events  = 0;

    return 1;
}

inline SignalEvent * IFdEventInterface::fdGetSignalSlot(int fd)
{
#ifdef USE_STL_MAP
    FileDescriptorMap::iterator it = this->fdMap.find(fd);

    if(it == fdMap.end())
        return NULL;

    fdnode * node = it->second;
    return node->OnEvent;
#else
    return this->fdarray[fd]->OnEvent;
#endif
}

inline size_t IFdEventInterface::getWatchedEvents()
{
    return this->watchedEvents;
}

inline int IFdEventInterface::fdPoll(int timeoutMs)
{
    return this->poll(timeoutMs);
}

inline int IFdEventInterface::fdGetReturnedEvents(size_t node_index)
{
    return this->getEventRevent(node_index);
}

inline int IFdEventInterface::fdGetFileDescriptor(size_t node_index)
{
    return this->getEventFileDescriptor(node_index);
}

inline int IFdEventInterface::fdGetNextFileDescriptor(int node_index)
{
    return this->getEventNextFileDescriptor(node_index);
}

inline int IFdEventInterface::isfdRegistered(int fd)
{
#ifdef USE_STL_MAP

    FileDescriptorMap::iterator it = this->fdMap.find(fd);

    if(it == fdMap.end())
        return 0;

#else

    if((size_t)fd >= this->maxfds)
        return 0;

    if(this->fdarray[fd] == NULL)
        return 0;
#endif

    return 1;

}

inline fd_event_handler IFdEventInterface::fdGetHandler(int fd)
{

#ifdef USE_STL_MAP
    FileDescriptorMap::iterator it = this->fdMap.find(fd);

    if(it == fdMap.end())
        return NULL;

    fdnode * node = it->second;
    return node->handler;
#else

    if (this->fdarray[fd] == NULL || this->fdarray[fd]->fd != fd)
    {
        util::CLogger::sharedLogger()->WriteFormated("Invalid handler: file %s and row %d:", __FILE__, __LINE__);
        return NULL;
    }

    return this->fdarray[fd]->handler;
#endif
}

inline void* IFdEventInterface::fdGetContext(int fd)
{

#ifdef USE_STL_MAP
    FileDescriptorMap::iterator it = this->fdMap.find(fd);

    if(it == fdMap.end())
        return NULL;

    fdnode * node = it->second;
    return node->ctx;
#else
    if (this->fdarray[fd] == NULL || this->fdarray[fd]->fd != fd )
    {
        util::CLogger::sharedLogger()->WriteFormated("Invalid context: file %s and row %d:", __FILE__, __LINE__);
        return NULL;
    }

    return this->fdarray[fd]->ctx;
#endif
}

}

#endif	/* IFDEVENTINTERFACE_INL_H */

